// 裁剪文字，显示...
function cutText(str, maxLength, showEllipsis) {
	if (str.length > maxLength) {
		str = str.substr(0, maxLength);
		if (showEllipsis) {
			str += "...";
		}
	}
	return str;
}

var wx = {


	// 判断微信内置浏览器
	isWeixin: function() {
		var ua = navigator.userAgent.toLowerCase();
		return (ua.match(/MicroMessenger/i) == "micromessenger");
	},

	// [Hack]修改iOS微信浏览器的title
	setTitle: function(t) {
		document.title = t;
		var i = document.createElement('iframe');
		i.src = '//m.baidu.com/favicon.ico';
		i.style.display = 'none';
		i.onload = function() {
			setTimeout(function() {
				i.remove();
			}, 9);
		}
		document.body.appendChild(i);
	},

	// 微信分享
	shareToWeiXin: function(param, success, cancel, mSuccess, mCancel) {
		var self = this;
		// this.getContent(utils.getQueryString("infoId"),utils.getQueryString('salesmanId'));
		var url = location.href.split('#')[0];
		alert('验证签名url == ' + url)
		alert('分享参数param==' + JSON.stringify(param));
		$.ajax({
			url: '/plug/do/common/weixin/genWxConfig',
			type: 'post',
			data: {
				url: url,
				authType: '1'
			},
			dataType: 'json',
			success: function(data) {
				if (data && data.resultCode === '000') {
					var config = data.data.config;
					config.debug = true;
					config.jsApiList = ['checkJsApi', 'showMenuItems', 'onMenuShareAppMessage',
						'onMenuShareTimeline'
					];
					wx.config(config);
					wx.ready(function() {
						wx.showMenuItems({
							menuList: ["menuItem:share:appMessage",
								"menuItem:share:timeline"
							]
						});
						wx.onMenuShareAppMessage({ //分享朋友
							title: param.infoTitle,
							desc: param.infoSummary, // 分享描述
							imgUrl: param.imgUrl,
							link: param.link, // 分享链接
							success: function() {
								success && success();
							},
							cancel: function() {
								cancel && cancel();
							}
						});

						wx.onMenuShareTimeline({ //分享朋友圈
							title: param.infoTitle,
							desc: param.infoSummary, // 分享描述
							imgUrl: param.imgUrl,
							link: param.link, // 分享链接
							success: function() {
								mSuccess && mSuccess();
							},
							cancel: function() {
								mCancel && mCancel();
							}
						});
					});

					wx.error(function(res) {
						alert('error :' + JSON.stringify(res));
					});
				}
			},
			error: function(err) {
				alert("err:" + JSON.stringify(err))
				console.log('请求页面报错: ' + err);
			}
		});
	},

	// 微信授权
	getUserInfo: function(success) {
		var self = this;
		var url = location.href;
		var wxCode = utils.getQueryString("code");
		alert('wxCode==' + wxCode);
		Backbone.ajax({
			url: '/plug/do/A2ATApp/saveCustomerInfo',
			dataType: "JSON",
			type: "post",
			data: {
				code: wxCode,
				operationSystem: 'IOS-test',
				network: '4G-test',
				operatorType: 'operatorType',
				models: 'models',
				appVersion: 'appVersion'
			},
			success: function(data) {
				if (data && data.resultCode == '000') {

					success && success();
				} else {
					alert('userInfo=' + JSON.stringify(data));
				}
			},
			error: function(e) {
				alert('error==' + JSON.stringify(e));
			}
		});

	},

	// 获取页面停留时间,依赖jquery.cookie.js
	countStayTime: function() {
		var second = 0;
		window.setInterval(function() {
			second++;
		}, 1000);
		var tjArr = localStorage.getItem("jsArr") ? localStorage.getItem("jsArr") : '[{}]';
		$.cookie('tjRefer', getReferrer(), {
			expires: 1,
			path: '/'
		});

		window.onbeforeunload = function() {
			if ($.cookie('tjRefer') == '') {
				var tjT = eval('(' + localStorage.getItem("jsArr") + ')');
				if (tjT) {
					tjT[tjT.length - 1].time += second;
					var jsArr = JSON.stringify(tjT);
					localStorage.setItem("jsArr", jsArr);
				}
			} else {
				var tjArr = localStorage.getItem("jsArr") ? localStorage.getItem("jsArr") : '[{}]';
				var dataArr = {
					'url': location.href,
					'time': second,
					'refer': getReferrer() || document.referrer,
					'timeIn': Date.parse(new Date()),
					'timeOut': Date.parse(new Date()) + (second * 1000)
				};
				tjArr = eval('(' + tjArr + ')');
				tjArr = JSON.stringify(dataArr);
				localStorage.setItem("jsArr", tjArr);
			}
			var standTime = (JSON.parse(localStorage.getItem('jsArr'))).time;
			return standTime; //单位：s
		};

	},

	// 获取前一个页面的url
	getReferrer: function() {
		var referrer = '';
		try {
			referrer = window.top.document.referrer;
		} catch (e) {
			if (window.parent) {
				try {
					referrer = window.parent.document.referrer;
				} catch (e2) {
					referrer = '';
				}
			}
		}
		if (referrer === '') {
			referrer = document.referrer;
		}
		return referrer;
	},

	//屏蔽alert 弹框
	noAlert: function() {
		//if(true) return
		window.alert = function(str) {
			return;
		};
	},

};
